local app = require 'app'
local game = app.game.running
local util = game.require 'util.util'

local handouter = game.ui.element()

handouter.store_watchers = 
{
    ingame = 
    {
        ['$'] = function(self)
            self:hide()
        end,

        ['请求自己出牌'] = function(self, store, state, args)
            local action_ids = args.action_ids
            local time = args.time

            self:on_handout(action_ids, time)

            local seat = self.ingame.seats[1]
            seat.poker_container:clear_chupai()
            seat:mark_pass(false)
        end,

        ['请求玩家出牌'] = function(self, store, state, args)
            self:hide()
        end,

        ['玩家出牌'] = function(self, store, state, args)
            self:hide()
        end
    },

    pokers = 
    {
        ['$'] = function() end,

        ['手牌选中'] = function(self, store, state, args)
            self:on_selected_change(state.selected_pokers)
        end
    }
}

function handouter:ctor(ingame)
    self.ingame = ingame
    self.res = "club.threecard.doudizhu/ui/ingame.b:handouter"
    self.legal = false
    self.tip_index = 0
end

function handouter:on_injected(bind)
    bind('button.click', self.pass_btn, self.pass_ivk)
    bind('button.click', self.tip_btn, self.tip_ivk)
    bind('button.click', self.handout_btn, self.handout_ivk)
    bind('button.click', self.forcepass_btn, self.forcepass_ivk)

    game.utility.button_style.highlight_scale(self.pass_btn)
    game.utility.button_style.highlight_scale(self.tip_btn)
    game.utility.button_style.highlight_scale(self.handout_btn)
    game.utility.button_style.highlight_scale(self.forcepass_btn)
end

function handouter:hide()
    self:super('hide')
    self.tip_index = 0
end

function handouter:on_handout(action_ids, time)
    self:show()
    self.action_ids = action_ids
    self.ingame.clock:start(time, self.clock_root)
    local _pass = false
    for _, action_id in ipairs(action_ids) do
        if action_id == util.action2id('pass') then
            _pass = true
            break
        end
    end

    if #action_ids == 1 and _pass then
        self.forcepass_btn.gameObject:SetActive(true)
        self.pass_btn.gameObject:SetActive(false)
        self.tip_btn.gameObject:SetActive(false)
        self.handout_btn.gameObject:SetActive(false)
    else
        self.forcepass_btn.gameObject:SetActive(false)
        self.pass_btn.gameObject:SetActive(_pass)
        self.tip_btn.gameObject:SetActive(true)
        self.handout_btn.gameObject:SetActive(true)
    end

    self.legal_obj:SetActive(self.legal)
    self.unlegal_obj:SetActive(not self.legal)
end

function handouter:pass_ivk()
    game.kbe.player():handout({})
    self:hide()
end

function handouter:tip_ivk()
    local action_ids = {}
    for _, action_id in ipairs(self.action_ids) do
        if action_id ~= util.action2id('pass') then
            table.insert(action_ids, action_id)
        end
    end
    self.tip_index = self.tip_index + 1

    self.tip_index = self.tip_index%#action_ids + 1
    action_id = action_ids[self.tip_index]
    local action = util.id2action(action_id)
    local client_values = util.action2client(action)
    local seat = self.ingame.seats[1]
    if seat then
        seat.poker_container:select_poker_by_client(client_values)
    end
end

function handouter:handout_ivk()
    if not self.legal then
        return
    end

    local card_ids = {}
    for _, poker in ipairs(game.store.pokers.state.selected_pokers) do
        table.insert(card_ids, poker.data.server_value)
    end
    game.kbe.player():handout(card_ids)
    self:hide()
end

function handouter:forcepass_ivk()
    game.kbe.player():handout({})
    self:hide()
end

function handouter:on_selected_change(selected_pokers)
    if not self.action_ids then
        return
    end
    local poker_datas = {}
    for _, poker in ipairs(selected_pokers) do
        table.insert(poker_datas, poker.data)
    end
    local action_id = util.cards2actionid(poker_datas)
    local legal = false
    for _, id in ipairs(self.action_ids) do
        if action_id == id then
            legal = true
            break
        end
    end

    self.legal = legal
    self.legal_obj:SetActive(self.legal)
    self.unlegal_obj:SetActive(not self.legal)
end

function handouter:clear_scene()
    self:hide()
end

return handouter